// var i;
// var $resKeeper = require("./ResKeeper");
// var $logic_ChildMoveEvent = require("./Logic_ChildMoveEvent");
// var $item_GameBagCell = require("./Item_GameBagCell");
// var u = cc._decorator;
// var d = u.ccclass;
// var p = u.property;
// var h = u.inspector;
// var f = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.cellFatherNode = null;
//         t.lastCubeArray = [];
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.StartSet = function (e) {
//         this.uiBag = e;
//         this.childMove = this.cellFatherNode.getComponent($logic_ChildMoveEvent.default);
//         if (!this.childMove) {
//             this.childMove = this.cellFatherNode.addComponent($logic_ChildMoveEvent.default);
//         }
//         this.childMove.StartSet(
//             this.onChildTouchStart.bind(this),
//             this.onChildTouchMove.bind(this),
//             this.onChildTouchUp.bind(this)
//         );
//         this.childMove.SetCantClick(!1);
//     };
//     t.prototype.SetCanMove = function (e, t) {
//         if (e) {
//             if (t) {
//                 var o = this.uiBag.auto_ui.node_cubeBaseFather.children[
//                     this.cellFatherNode.children[0].getComponent($item_GameBagCell.default).cellData.cellIndex
//                 ].convertToWorldSpaceAR(cc.v2(0, 0));
//                 var n = this.cellFatherNode.children[0].convertToWorldSpaceAR(cc.v2(0, 0));
//                 var i = cc.v2(o.x - n.x, o.y - n.y);
//                 this.node.x += i.x;
//                 this.node.y += i.y;
//                 this.startPos = this.node.getPosition();
//                 this.childMove.SetCantClick(!0);
//             }
//             this.checkEndData = null;
//         } else {
//             this.childMove.SetCantClick(!1);
//         }
//     };
//     t.prototype.onChildTouchStart = function () {};
//     t.prototype.onChildTouchMove = function (e) {
//         this.node.x += e.getDeltaX();
//         this.node.y += e.getDeltaY();
//         this.CheckCellIsOk();
//     };
//     t.prototype.onChildTouchUp = function () {
//         var e = this;
//         var t = this.CheckCellIsOk();
//         this.lastCubeArray.forEach(function (t) {
//             e.uiBag.baseCellCompArray[t].SetBgNodeColor(cc.Color.WHITE);
//         });
//         if (t.isOk) {
//             this.checkEndData = t;
//             this.node.x += t.effectPos.x;
//             this.node.y += t.effectPos.y;
//             this.startPos = this.node.getPosition();
//             this.uiBag.MoveChangeBagCellIndex(this.checkEndData);
//         } else {
//             this.node.setPosition(this.startPos);
//             this.checkEndData = null;
//         }
//         this.lastCubeArray.length = 0;
//     };
//     t.prototype.CheckCellIsOk = function () {
//         var e = this;
//         this.lastCubeArray.forEach(function (t) {
//             e.uiBag.baseCellCompArray[t].SetBgNodeColor(cc.Color.WHITE);
//         });
//         var t = this.cellFatherNode.children;
//         var o = this.uiBag.auto_ui.node_cubeBaseFather.children;
//         var n = [];
//         var i = [];
//         var a = [];
//         t.forEach(function (e) {
//             n.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
//         });
//         o.forEach(function (e) {
//             i.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
//         });
//         var r = !0;
//         var c = null;
//         this.lastCubeArray.length = 0;
//         for (var s = 0; s < t.length; s++) {
//             var l = n[s];
//             var u = !1;
//             var d = function (e) {
//                 var t = !1;
//                 if (p.uiBag.currAddCellDataMap.size > 0) {
//                     p.uiBag.currAddCellDataMap.forEach(function (o) {
//                         if (o.pickCellArray && !t)
//                             for (var n = 0; n < o.pickCellArray.length; n++) {
//                                 var i = o.pickCellArray[n];
//                                 if (e == i.cellData.cellIndex) {
//                                     t = !0;
//                                     break;
//                                 }
//                             }
//                     });
//                 }
//                 if (t) {
//                     return "continue";
//                 }
//                 var o = i[e];
//                 if (cc.Vec2.distance(l, o) < p.uiBag.effectMax) {
//                     return (
//                         (u = !0), p.lastCubeArray.push(e), a.push(e), c || (c = cc.v2(o.x - l.x, o.y - l.y)), "break"
//                     );
//                 } else {
//                     return void 0;
//                 }
//             };
//             var p = this;
//             for (var h = 0; h < o.length && "break" !== d(h); h++) {}
//             if (!u) {
//                 r = !1;
//             }
//         }
//         this.lastCubeArray.forEach(function (t) {
//             e.uiBag.baseCellCompArray[t].SetBgNodeColor(r ? cc.Color.GREEN : cc.Color.RED);
//         });
//         return {
//             isOk: r,
//             effectPos: c,
//             pickCellArray: a
//         };
//     };
//     __decorate([p(cc.Node)], t.prototype, "cellFatherNode", void 0);
//     return __decorate([d], t);
// })($resKeeper.default);
// exports.default = f;
import ResKeeper from "./ResKeeper";
import LogicChildMoveEvent from "./Logic_ChildMoveEvent";
import ItemGameBagCell from "./Item_GameBagCell";
import ViewGameBag from "./UI_Bag";

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名: ResKeeperComponent
 * 资源保持器组件，包含用于管理和处理子节点移动的逻辑
 */
@ccclass
class ItemCellFatherMove extends ResKeeper {
    @property(cc.Node)
    public cellFatherNode: cc.Node = null; // 父节点对象

    private lastCubeArray: number[] = [];
    private uiBag: ViewGameBag;
    private childMoveEvent: LogicChildMoveEvent;
    private initialPosition: cc.Vec2 = null;
    private checkEndResult: any = null;

    constructor() {
        super();
    }

    /**
     * 初始化设置
     * @param uiBag - 传入的UI包
     * 建议方法名: initializeSettings
     */
    public initializeSettings(uiBag: any): void {
        this.uiBag = uiBag;
        this.childMoveEvent = this.cellFatherNode.getComponent(LogicChildMoveEvent);
        if (!this.childMoveEvent) {
            this.childMoveEvent = this.cellFatherNode.addComponent(LogicChildMoveEvent);
        }
        this.childMoveEvent.startSet(
            this.handleChildTouchStart.bind(this),
            this.handleChildTouchMove.bind(this),
            this.handleChildTouchEnd.bind(this)
        );
        this.childMoveEvent.setCantClick(false);
    }

    /**
     * 设置是否可以移动
     * @param canMove - 是否可以移动
     * @param adjustPosition - 是否需要调整位置
     * 建议方法名: setMoveability
     */
    public setMoveability(canMove: boolean, adjustPosition: boolean): void {
        if (canMove) {
            if (adjustPosition) {
                const targetNode = this.uiBag.auto_ui.node_cubeBaseFather.children[
                    this.cellFatherNode.children[0].getComponent(ItemGameBagCell).cellData.cellIndex
                ];
                const targetPos = targetNode.convertToWorldSpaceAR(cc.v2(0, 0));
                const currentPos = this.cellFatherNode.children[0].convertToWorldSpaceAR(cc.v2(0, 0));
                const positionOffset = cc.v2(targetPos.x - currentPos.x, targetPos.y - currentPos.y);
                this.node.x += positionOffset.x;
                this.node.y += positionOffset.y;
                this.initialPosition = this.node.getPosition();
                this.childMoveEvent.setCantClick(true);
            }
            this.checkEndResult = null;
        } else {
            this.childMoveEvent.setCantClick(false);
        }
    }

    /**
     * 子节点触摸开始事件的回调函数
     * 建议方法名: handleChildTouchStart
     */
    private handleChildTouchStart(): void { }

    /**
     * 子节点触摸移动事件的回调函数
     * @param touchEvent - 触摸事件
     * 建议方法名: handleChildTouchMove
     */
    private handleChildTouchMove(touchEvent: cc.Event.EventTouch): void {
        this.node.x += touchEvent.getDeltaX();
        this.node.y += touchEvent.getDeltaY();
        this.validateCell();
    }

    /**
     * 子节点触摸结束事件的回调函数
     * 建议方法名: handleChildTouchEnd
     */
    private handleChildTouchEnd(): void {
        const isCellOkResult = this.validateCell();
        this.lastCubeArray.forEach(index => {
            this.uiBag.baseCellCompArray[index].updateBgNodeColor(cc.Color.WHITE);
        });
        if (isCellOkResult.isOk) {
            this.checkEndResult = isCellOkResult;
            this.node.x += isCellOkResult.effectPos.x;
            this.node.y += isCellOkResult.effectPos.y;
            this.initialPosition = this.node.getPosition();
            this.uiBag.moveChangeBagCellIndex(this.checkEndResult);
        } else {
            this.node.setPosition(this.initialPosition);
            this.checkEndResult = null;
        }
        this.lastCubeArray.length = 0;
    }

    /**
     * 检查单元格是否有效
     * @returns 检查结果
     * 检查结果包含是否有效、偏移位置以及选择的单元格数组
     * 建议方法名: validateCell
     */
    private validateCell(): { isOk: boolean, effectPos: cc.Vec2, pickCellArray: number[] } {
        //     this.lastCubeArray.forEach(index => {
        //         this.uiBag.baseCellCompArray[index].SetBgNodeColor(cc.Color.WHITE);
        //     });

        //     const childNodes = this.cellFatherNode.children;
        //     const baseNodes = this.uiBag.auto_ui.node_cubeBaseFather.children;
        //     const childWorldPositions: cc.Vec2[] = [];
        //     const baseWorldPositions: cc.Vec2[] = [];
        //     const pickCellArray: number[] = [];

        //     childNodes.forEach(child => {
        //         childWorldPositions.push(child.convertToWorldSpaceAR(cc.v2(0, 0)));
        //     });

        //     baseNodes.forEach(base => {
        //         baseWorldPositions.push(base.convertToWorldSpaceAR(cc.v2(0, 0)));
        //     });

        //     let isOk = true;
        //     let effectPos: cc.Vec2 = null;
        //     this.lastCubeArray.length = 0;

        //     for (let i = 0; i < childNodes.length; i++) {
        //         const childPos = childWorldPositions[i];
        //         let found = false;

        //         for (let j = 0; j < baseNodes.length; j++) {
        //             const basePos = baseWorldPositions[j];
        //             if (cc.Vec2.distance(childPos, basePos) < this.uiBag.effectMax) {
        //                 found = true;
        //                 this.lastCubeArray.push(j);
        //                 pickCellArray.push(j);
        //                 if (!effectPos) {
        //                     effectPos = cc.v2(basePos.x - childPos.x, basePos.y - childPos.y);
        //                 }
        //                 break;
        //             }
        //         }

        //         if (!found) {
        //             isOk = false;
        //         }
        //     }

        //     this.lastCubeArray.forEach(index => {
        //         this.uiBag.baseCellCompArray[index].SetBgNodeColor(isOk ? cc.Color.GREEN : cc.Color.RED);
        //     });

        //     return {
        //         isOk,
        //         effectPos,
        //         pickCellArray
        //     };
        this.lastCubeArray.forEach((t) => {
            this.uiBag.baseCellCompArray[t].updateBgNodeColor(cc.Color.WHITE);
        });
        const childNodes = this.cellFatherNode.children;
        const baseNodes = this.uiBag.auto_ui.node_cubeBaseFather.children;
        const childWorldPositions = [];
        const baseWorldPositions = [];
        const pickCellArray = [];
        childNodes.forEach((e) => {
            childWorldPositions.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
        });
        baseNodes.forEach((e) => {
            baseWorldPositions.push(e.convertToWorldSpaceAR(cc.v2(0, 0)));
        });
        let isOk = true;
        let effectPos = null;
        this.lastCubeArray.length = 0;
        for (var s = 0; s < childNodes.length; s++) {
            var childPos = childWorldPositions[s];
            var found = false;
            var d = (e) => {
                var t = !1;
                if (this.uiBag.currAddCellDataMap.size > 0) {
                    this.uiBag.currAddCellDataMap.forEach((o) => {
                        if (o.pickCellArray && !t)
                            for (var n = 0; n < o.pickCellArray.length; n++) {
                                var i = o.pickCellArray[n];
                                if (e == i.cellData.cellIndex) {
                                    t = !0;
                                    break;
                                }
                            }
                    });
                }
                if (t) {
                    return "continue";
                }
                var o = baseWorldPositions[e];
                if (cc.Vec2.distance(childPos, o) < this.uiBag.effectMax) {
                    return (
                        (found = !0), this.lastCubeArray.push(e), pickCellArray.push(e), effectPos || (effectPos = cc.v2(o.x - childPos.x, o.y - childPos.y)), "break"
                    );
                } else {
                    return void 0;
                }
            };
            for (var h = 0; h < baseNodes.length && "break" !== d(h); h++) { }
            if (!found) {
                isOk = !1;
            }
        }
        this.lastCubeArray.forEach((t) => {
            this.uiBag.baseCellCompArray[t].updateBgNodeColor(isOk ? cc.Color.GREEN : cc.Color.RED);
        });
        return {
            isOk: isOk,
            effectPos: effectPos,
            pickCellArray: pickCellArray
        };
    }

}

export default ItemCellFatherMove;